﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProjectLibrary;

namespace XNAProject
{
    public sealed class TitleIntroState : BaseGameState, ITitleIntroState
    {
        private SpriteFont font;

        private Texture2D introAlpha;
        private Texture2D introColor;

        private Vector2 position;

        private float fadeHelper = 0.0f;
        private float fadeLogo = 255;

        public TitleIntroState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(ITitleIntroState), this);                                       //register service
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.Start, Keys.Escape)                                             //input handling
                || Input.WasPressed(0, Buttons.Start, Keys.Enter)
                || Input.WasPressed(0, Buttons.Start, Keys.Space))
            {
                GameManager.ChangeState(OurGame.StartMenuState.Value);                
            }

            fadeHelper += (float)gameTime.ElapsedGameTime.Milliseconds / 10;

            if (fadeHelper >= 1.0f)
            {
                fadeHelper = 0;
                fadeLogo--;
                if (fadeLogo <= 0)
                    fadeLogo = 0;
            }

            base.Update(gameTime);
        }

        protected override void LoadContent()
        {
            //OurGame.Sound.Play("test");                                                                     //play sound

            font = Content.Load<SpriteFont>(@"Fonts\description");                                          //load font

            introAlpha = Content.Load<Texture2D>("Textures/Intro/introAlpha");
            introColor = Content.Load<Texture2D>("Textures/Intro/introColor");

            Vector2 center = new Vector2(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
            position = new Vector2(center.X - introColor.Width / 2, center.Y - introColor.Height / 2);

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.GraphicsDevice.Clear(new Color((byte)53, (byte)76, (byte)108, (byte)255));
            
            OurGame.spriteBatch.Draw(introAlpha, position, new Color((byte)255, (byte)255, (byte)255, (byte)(fadeLogo)));
            OurGame.spriteBatch.Draw(introColor, position, new Color((byte)255, (byte)255, (byte)255, (byte)fadeLogo));

            OurGame.spriteBatch.DrawString(font, "TitleIntroState  \n press Esc or Enter or Space for StartMenuState", new Vector2(20, 20), Color.White);

            OurGame.spriteBatch.End();                                                                      

            base.Draw(gameTime);
        }

        

    }
}
